'''
Created on Jun 2, 2011

@author: Jialai_Zhu
'''
import unittest
import PyV8
class GlobalHook(PyV8.JSClass):
    def toString(self):
        print 'toString'
        return PyV8.JSClass.toString(self)   
    def toLocaleString(self):
        print 'toLocaleString'
        return PyV8.JSClass.toLocaleString(self)
    
    def valueOf(self):
        return self
    
    def hasOwnProperty(self, name):
        print name
        return PyV8.JSClass.hasOwnProperty(self, name)    
    def isPrototypeOf(self, obj):
        "Returns a Boolean value indicating whether an object exists in the prototype chain of another object."
        PyV8.JSClass.isPrototypeOf(self, obj)    
    def __defineGetter__(self, name, getter):
        print name
        return PyV8.JSClass__defineGetter__(self, name, getter)
    def __lookupGetter__(self, name):
        print name
        return PyV8.JSClass.__lookupGetter__(self, name)    
    def __defineSetter__(self, name, setter):
        print name
        return PyV8.JSClass.__defineSetter__(self, name, setter)
    def __lookupSetter__(self, name):
        print name
        return PyV8.JSClass.__lookupSetter__(self, name)
        
class Global(GlobalHook):      # define a compatible javascript class
    def __init__(self):               # define a method
        class document(GlobalHook):
            class cookie(GlobalHook):
                def __init(self):pass
            cookie=cookie()
        class location(GlobalHook):
            def __init__(self,Global):
                self.Global=Global
            def reload(self):
                print 'location.reload'
                print self.Global.document.cookie
            def assign(self,url):
                print url
        self.location=location(self)
        self.document=document()
    def report(self):
        try:
            print self.document.cookie
        except:
            pass
        
global_oject=Global()
ctxt = PyV8.JSContext(global_oject)          # create a context with an implicit global object
ctxt.enter()                     # enter the context (also support with statement)

code2="var td=new Date();td.setDate(td.getDate()+30);document.cookie='checkcode=bnmp912873;expires='+td.toGMTString()+';path=/;';location.reload();"
ctxt.eval(code2)                 # evalute the javascript expression

code3='''var mycookie="87c85f0f4a1433cf2fb421769f26407690aab46053d911e1";
var td=new Date();
td.setDate(td.getDate()+30);
document.cookie="yetet3uu=checkcode="+mycookie+";expires="+td.toGMTString()+";path=/;";
var myurl="/index.asp";
location=myurl;
'''
print ctxt.eval(code3)                 # evalute the javascript expression
vars=ctxt.locals
cookie=vars.document.cookie
location=vars.location

 

if __name__ == "__main__":
    pass